package spotGUI.personalProfileGUI;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;

import managers.DBManager;
import managers.OptionsManager;
import managers.socialNetworkManagers.CacheManager;
import managers.socialNetworkManagers.ElementsManager;
import managers.socialNetworkManagers.SocialNetworkFeedbackManager;
import managers.socialNetworkManagers.SocialNetworkManager;
import net.tomahawk.ExtensionsFilter;
import net.tomahawk.XFileDialog;
import personalProfile.PhotoManager;
import personalProfile.PhotoManager.Photo;
import spotGUI.utils.EmptyContentAreaButton;
import utils.SocialUserData;
import utils.StackTracePrinter;
import elements.Element;
import java.util.ResourceBundle;

public class LittlePhotoView extends JPanel {

	private Box horizontalBox;
	private JButton btnShow;
	private JButton btnRemove;
	private JButton btnDownload;
	private Component horizontalGlue;

	private Photo photo;
	private PhotoManager manager;
	private JButton btnShare;


	public LittlePhotoView(final Photo photo, final PhotoManager manager, boolean optionsEnabled) {

		this.photo = photo;
		this.manager = manager;

		setLayout(new BorderLayout(0, 0));

		JLabel photoLabel = new JLabel (photo.image);
		photoLabel.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
		photoLabel.setHorizontalTextPosition(SwingConstants.CENTER);
		photoLabel.setToolTipText(photo.description);
		add(photoLabel, BorderLayout.CENTER);

		horizontalBox = Box.createHorizontalBox();
		add(horizontalBox, BorderLayout.SOUTH);

		horizontalGlue = Box.createHorizontalGlue();
		horizontalBox.add(horizontalGlue);

		btnShow = new EmptyContentAreaButton(LittlePhotoView.class.getResource("/images/stdIcons24/monitor.png"), "");
		btnShow.setVisible(false);
		btnShow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				manager.showBigPhoto(photo);
			}
		});
		btnShow.setPreferredSize(new Dimension(30, 30));
		btnShow.setToolTipText(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("LittlePhotoView.btnShow.toolTipText")); //$NON-NLS-1$ //$NON-NLS-2$
		horizontalBox.add(btnShow);

		btnRemove = new EmptyContentAreaButton(LittlePhotoView.class.getResource("/images/stdIcons24/delete.png"), "");
		btnRemove.setVisible(false);
		btnRemove.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				removePhoto();
			}
		});
		btnRemove.setPreferredSize(new Dimension(30, 30));
		btnRemove.setToolTipText(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("LittlePhotoView.btnRemove.toolTipText")); //$NON-NLS-1$ //$NON-NLS-2$
		horizontalBox.add(btnRemove);

		btnDownload = new EmptyContentAreaButton(LittlePhotoView.class.getResource("/images/stdIcons24/download.png"), "");
		btnDownload.setVisible(false);
		btnDownload.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				savePhoto();
			}
		});
		btnDownload.setPreferredSize(new Dimension(30, 30));
		btnDownload.setToolTipText(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("LittlePhotoView.btnDownload.toolTipText")); //$NON-NLS-1$ //$NON-NLS-2$
		horizontalBox.add(btnDownload);

		btnShare = new EmptyContentAreaButton(LittlePhotoView.class.getResource("/images/stdIcons24/wired.png"), "");
		btnShare.setVisible(false);
		btnShare.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				sharePhoto();
			}
		});
		btnShare.setToolTipText(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("LittlePhotoView.btnShare.toolTipText")); //$NON-NLS-1$ //$NON-NLS-2$
		horizontalBox.add(btnShare);

		if (optionsEnabled)
			enableOptions();
	}


	public void sharePhoto () {

		String message = JOptionPane.showInputDialog(this, ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("LittlePhotoView.share1"), ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("LittlePhotoView.share2"), JOptionPane.PLAIN_MESSAGE);
		if (message == null)
			message = "";
		String albumName = manager.getAlbums().get(photo.albumId).name;
		SocialUserData owner = SocialNetworkManager.getSharedInstance().getUser(manager.getUserId());
		String ownerName = owner.getFirstName()+" "+owner.getSurname();

		try {
			ElementsManager.addElement(DBManager.getConnection(), new Element(Element.PHOTO, SocialNetworkManager.getSharedInstance().getUserId(),
					"Fotografia", "Dall'album "+albumName+"\ndi "+ownerName+"\n\n"+message, photo.image));
		} catch (SQLException e) {
			SocialNetworkManager.getSharedInstance().getFeedbackManager().updateStatus(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("LittlePhotoView.shareError"), SocialNetworkFeedbackManager.WARNING);
			StackTracePrinter.print(e);
		}
	}

	public void removePhoto () {
		manager.addPendingRemovePhoto(photo);
	}

	private void savePhoto() {
		File input = null;
		File output = null; 
		try {
			input = new File(OptionsManager.getSharedInstance().getOptionGroup("fileTransfer").get("cacheFolder")+"/"+CacheManager.formatPhotoName(photo.photoId, CacheManager.LARGE));
			if (!input.exists())
				manager.retrieveBigPhoto(photo, DBManager.getConnection());
			XFileDialog fileChooser = new XFileDialog("Salva");
			Vector<String> filter = new Vector<String>();
			filter.add("jpg");
			fileChooser.addFilters(new ExtensionsFilter("Immagine jpeg", filter));
			String fileName = fileChooser.getSaveFile();
			if (!fileName.endsWith(".jpg"))
				fileName+=".jpg";
			String directory = fileChooser.getDirectory();
			output = new File (directory+fileName);
			if (fileName != null) {
				output.createNewFile();
				CacheManager.copyfile(input, output);
			}
		} catch (Exception e) {
			StackTracePrinter.print(e);
			JOptionPane.showConfirmDialog(this, ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("LittlePhotoView.saveError"),
					ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("LittlePhotoView.saveError2"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE);
			output.delete();

		}
	}


	public void enableOptions () {
		btnShare.setVisible(true);
		btnShow.setVisible(true);
		btnDownload.setVisible(true);
	}

	public void enablePhotoRemoval () {
		btnRemove.setVisible(true);
	}

}
